#define _CRT_SECURE_NO_WARNINGS
#include "doublelist.h"

DLN* DLNinit()
{
	DLN* phead = (DLN*)malloc(sizeof(DLN));
	phead->date = -1;
	phead->prevent = phead;
	phead->next = phead;
	return phead;
}

DLN* BuyNewNode(DLNType x)
{
	DLN* newnode = (DLN*)malloc(sizeof(DLN));
	newnode->date = x;
	newnode->prevent = NULL;
	newnode->next = NULL;
	return newnode;
}

void DLNprint(DLN* phead)
{
	DLN* cur = phead->next;
	while (cur != phead)
	{
		printf("<=%d=>", cur->date);
		cur = cur->next;
	}
	printf("\n");
}

void DLNInsert(DLN* pos, DLNType x)
{
	assert(pos);
	DLN* newnode = BuyNewNode(x);
	pos->prevent->next = newnode;
	newnode->prevent = pos->prevent;
	pos->prevent = newnode;
	newnode->next = pos;
}

void DLNErase(DLN* pos)
{
	assert(pos);
	assert(pos->next != pos);
	pos->prevent->next = pos->next;
	pos->next->prevent = pos->prevent;
	free(pos);
	pos = NULL;
}

void DLNPushBack(DLN* phead, DLNType x)
{
	assert(phead);
	DLNInsert(phead,x);
}

void DLNPopBack(DLN* phead)
{
	assert(phead);
	DLNErase(phead->prevent);
}

void DLNPushFront(DLN* phead, DLNType x)
{
	assert(phead);
	DLNInsert(phead->next,x);
}

void DLNPopFront(DLN* phead)
{
	assert(phead);
	DLNErase(phead->next);
}

void DLNDestory(DLN** phead)
{
	DLN* cur = (*phead)->next;
	while (cur!=*phead)
	{
		DLN* next = cur->next;
		free(cur);
		cur = NULL;
		cur = next;
	}
	free(*phead);
	*phead = NULL;
}

